
function request(url, params, success, fail) {
  this.requestLoading(url, params, "", success, fail)
}
// 展示进度条的网络请求
// url:网络请求的url
// params:请求参数
// message:进度条的提示信息
// success:成功的回调函数
// fail：失败的回调
function requestLoading(url, params, message, success, fail) {
  console.log(params)
  wx.showNavigationBarLoading()
  if (message != "") {
    wx.showLoading({
      title: message,
      duration:2000
    })
  }
  wx.request({
    url: url,
    data: params,
    header: {
      //'Content-Type': 'application/json'
      'content-type': 'application/x-www-form-urlencoded'
    },
    method: 'post',
    success: function (res) {
      //console.log(res.data)
      wx.hideNavigationBarLoading()
      if (message != "") {
        wx.hideLoading()
      }
      if (res.statusCode == 200) {
        success(res.data)
      } else {
        fail()
      }

    },
    fail: function (res) {
      wx.hideNavigationBarLoading()
      if (message != "") {
        wx.hideLoading()
      }
      fail()
    },
    complete: function (res) {

    },
  })
}

var error = function (error) {
  wx.hideLoading();
  wx.showToast({
    title: '加载数据失败',
    icon: 'none',
    duration: 2000
  })
}

/**
 * 分三位数转逗号 
 */
function format_number(n) {
  var b = parseInt(n).toString();
  var len = b.length;
  if (len <= 3) { return b; }
  var r = len % 3;
  return r > 0 ? b.slice(0, r) + "," + b.slice(r, len).match(/\d{3}/g).join(",") : b.slice(r, len).match(/\d{3}/g).join(",");
}

/* 
 *同期增减比
 */
function compareVal(basic, compare) {
  var d;
  if (compare == 'null' && basic != 'null') {
    d = 100;
  }
  if (compare != 'null' && basic == 'null') {
    d = -100;
  }
  if (compare == 'null' && basic == 'null') {
    d = 0;
  }
  if (compare != 'null' && basic != 'null') {
    d = (((parseFloat(basic) - parseFloat(compare)) / parseFloat(compare)) * 100).toFixed(1);
  }
  if (basic != 'null' && isNaN(parseInt(compare))) {
    d = 100;
  }
  if (compare != 'null' && isNaN(parseInt(basic))) {
    d = -100;
  }
  if (isNaN(parseInt(compare)) && isNaN(parseInt(basic))) {
    d = 0;
  }
  if (isNaN(parseInt(d))) {
    d = 0
  }
  return d
}
/* 
 *获取当前时间 例如：2018-10-23
 */
function CurentTime() {
  var now = new Date();

  var year = now.getFullYear();       //年
  var month = now.getMonth() + 1;     //月
  var day = now.getDate();            //日

  var hh = now.getHours();            //时
  var mm = now.getMinutes();          //分

  var clock = year + "-";

  if (month < 10)
    clock += "0";

  clock += month + "-";

  if (day < 10)
    clock += "0";

  clock += day + " ";

  // if(hh < 10)
  //     clock += "0";

  // clock += hh + ":";
  // if (mm < 10) clock += '0'; 
  // clock += mm; 
  return (clock);
} 

/**
 * x轴与y轴意义对应
 * 
 */
function timesDataKey(times, counts, WCustTrafficArr, sales) {
  for (var i = 0; i < counts.length; i++) {
    var lab = counts[i].LABEL;
    var n = times.indexOf(lab);
    WCustTrafficArr[n] = counts[i].IN; //客流量

  }
  for (var j = 0; j < sales.length; j++) {
    var saleTime = sales[j].LABEL;
    var time = times.indexOf(saleTime);
    WcountsArr[time] = sales[j].Counts; //下单数
  }
}
/**
 * 成交率
*/
function cal(arr1, arr2) {
  var charts1Dayturn = [];
  for (var i = 0; i < arr1.length; i++) {
    var obj = ((arr1[i] / arr2[i]) * 100).toFixed(1);
    if (isNaN(parseFloat(obj))) {
      obj = 0
    }
    charts1Dayturn.push(obj);
  }
  return charts1Dayturn;
}

/**
 * call（） 成交率 arr为字符串
 * 主要是详情店铺列表引用
 */
function call(arr1, arr2) {
  var obj = ((arr1 / arr2) * 100).toFixed(1);
  if (isNaN(parseFloat(obj))) {
    obj = 0
  }
  return obj

}
/**
 *判断数组里面的值为未定义
*/
function calUndefined(arr){
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] == "null" || typeof (arr[i]) == "undefined" || arr[i] == '') {
      arr[i] = 0
    }
  }
}

module.exports = {
  request,
  requestLoading,
  format_number,
  compareVal,
  CurentTime,
  timesDataKey,
  cal ,//成交率
  call,
  calUndefined,
  error 
}

